'use strict'

import axios from 'axios'
const qs = require('qs')
// const Host = 'http://47.91.159.30:10086/index.php/client'; //测试服地址
// const Host = 'http://47.75.205.251:11111/client'; //正式服线上地址
const Host = 'http://47.96.115.178:11111/client'; // v2019/3/28
//添加请求拦截器
axios.interceptors.request.use(config => {
    return config
}, error => {
    return Promise.reject(error)
})

//添加响应拦截器
axios.interceptors.response.use(response => {
    return response
}, error => {
    return Promise.resolve(error.response)
})


//返回数据
function checkResponseData(response) {
    if (response && (response.status == 200 || response.status == 304)) {
        return stringToJson(response.data)
    }
    return {
        code: -404,
        message: response.statusText,
        data: response.statusText,
    }
}


//转字符串为JSON
function stringToJson(resData) {
    if (typeof (resData) == 'String') {
        resData = JSON.parse(resData);
    }
    return resData
}

export default {
    post(url, data) {
        if(sessionStorage.token){
            data.token = sessionStorage.token;
            data.uid = sessionStorage.uid;
        }
        return axios({
            method: 'post',
            baseURL: Host,
            url,
            data: qs.stringify(data),
            timeout: 10000,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            }
        }).then(
            (response) => {
                return checkResponseData(response)
            }
        ).catch(
            (err) => {
                return showErrorMsg(err)
            }
        )
    },
    formData(url, data) {
        if(sessionStorage.token){
            data.token = sessionStorage.token;
            data.uid = sessionStorage.uid;
        }
        return axios({
            method: 'post',
            baseURL: Host,
            url,
            data: data,
            timeout: 10000,
            headers: {
                'Content-Type': 'multipart/form-data; charset=UTF-8',
            }
        }).then(
            (response) => {
                return checkResponseData(response)
            }
        ).catch(
            (err) => {
                return showErrorMsg(err)
            }
        )
    },
    get(url, params) {
        let data = {};
        if(typeof(params) == 'undefined'){
            data.token = sessionStorage.getItem('token');
            data.uid = sessionStorage.uid;
        }else{
            if(sessionStorage.token){
                params.token = sessionStorage.token;
                params.uid = sessionStorage.uid;
                data = params;
            }
        }
        return axios({
            method: 'get',
            baseURL: Host,
            url,
            params:data, // get 请求时带的参数
            timeout: 10000,
        }).then(
            response => {
                return checkResponseData(response)
            }
        ).catch(
            err => {
                return err
            }
        )
    }
}
